package cn.dengta.webapp.trade.service;

import java.util.List;
import javax.annotation.Resource;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.webapp.base.service.BaseServiceImpl;
import cn.dengta.webapp.trade.dao.FemaleRecommendStatisticsDao;
import cn.dengta.webapp.trade.entity.*;
import cn.dengta.webapp.user.entity.User;
import cn.dengta.webapp.user.model.Gender;
import cn.dengta.webapp.user.model.UserInfoSupply;
import cn.dengta.webapp.user.service.RemoteUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * 女用户推荐分值统计 服务实现类
 *
 * @author zw
 * @since 2022-10-09
 */
@Service
public class FemaleRecommendStatisticsService extends BaseServiceImpl<FemaleRecommendStatistics, FemaleRecommendStatisticsDao, Long> {

	@Resource
	MaleRecommendStatisticsService maleRecommendStatisticsService;
	@Resource
	UserRecommendConfigService userRecommendConfigService;
	@DubboReference
	RemoteUserService remoteUserService;

	public void add(Long userId) {
		FemaleRecommendStatistics femaleRecommendStatistics = FemaleRecommendStatistics.of(userId);
		baseDao.insert(femaleRecommendStatistics);
	}

	public void addFamaleRecommend(FemaleRecommendStatistics femaleRecommendStatistics) {
		baseDao.insert(femaleRecommendStatistics);
	}

	public void addFieldValue(Long userId, String field, Integer score) {
		baseDao.addFieldValue(field, score, userId);
	}

	public void coverFieldValue(Long userId, String field, Integer score) {
		baseDao.coverFieldValue(field, score, userId);
	}

	public void coverTotleAndFieldValue(Long userId, String field, Integer score) {
		baseDao.coverTotleAndFieldValue(field, score, userId);
	}

	public List<User> selectUserInfoSupply(long maxId, int limit) {
		return baseDao.selectUserInfoSupply(maxId, limit);
	}

	public void resetFieldScore(String field) {
		baseDao.resetFieldScore(field);
	}

	public void updateTotalSort() {
		baseDao.updateTotalSort();
	}

	/**
	 * 音视频接通率
	 */
	public List<UserCallStatistics> callRate(TimeInterval addTime) {
		return baseDao.callRate(addTime);
	}

	/**
	 * 修改性别，重置推荐分值
	 */
	public void changeGender(User user) {
		// 清除之前女用户推荐记录
		maleRecommendStatisticsService.deleteById(user.getId());
		FemaleRecommendStatistics femaleRecommendStatistics = FemaleRecommendStatistics.of(user.getId());

		final UserRecommendConfig userRecommendConfig = userRecommendConfigService.getByType(Gender.FEMALE.getValue());
		final Integer info = userRecommendConfig.getInfo();
		final Integer avatar = userRecommendConfig.getAvatar();

		// 计算女用户资料完成度
		final int infoScore = UserInfoSupply.calcScore(user.getWesKit(),user.getInfoSupplyFlag()) * info / 100;
		// 真人认证
		final Integer avatarStatus = user.getAvatarStatus() == 2 ? avatar : 0;
		// 新增用户分值
		femaleRecommendStatistics.setInfo(infoScore);
		femaleRecommendStatistics.setAvatar(avatarStatus);
		addFamaleRecommend(femaleRecommendStatistics);
	}

	/**
	 * 资料完整度
	 */
	public void userInfo(Long infoSupplyFlag, User user) {
		if (user.getGender() == Gender.FEMALE) {
			final UserRecommendConfig userRecommendConfig = userRecommendConfigService.getByType(Gender.FEMALE.getValue());
			final int info = userRecommendConfig.getInfo();
			// 计算女用户资料完成度
			final int infoScoreOld = UserInfoSupply.calcScore(user.getWesKit(), infoSupplyFlag) * info / 100;
			final int infoScore = UserInfoSupply.calcScore(user.getWesKit(), user.getInfoSupplyFlag()) * info / 100;
			coverFieldValue(user.getId(), FemaleRecommendStatistics.INFO, infoScore);
			remoteUserService.updateSortById(user.getId(), infoScore - infoScoreOld);
		}
	}

}
